﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using YcdShared;
using YcdModel;
using System.Data;

namespace YcdWebApiService.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class YcdController : ControllerBase
    {
#if false
        // GET api/values
        [HttpGet]
        public ActionResult<IEnumerable<string>> Get()
        {
            return new string[] { "value3", "9999" };
        }

        // GET api/values/5
        //[HttpGet("{id}")]
        //public ActionResult<string> Get(int id)
        //{
        //    return "value";
        //}

        [HttpGet("{id}")]
        public ActionResult<object> Get(int id)
        {
            //return new InfoStudent { ID = id, NAME = "SKQ", AGE = 23, SEX = false };
            SQLConnServer.InitConnection("server=10.0.0.252;database=TRTEST;uid=sa;pwd=fp5zj8hkA");
            var db = SQLConnServer.PopConn("PACKING");
            var table = SQLCmd.Select($"SELECT TOP {id} * FROM PACKING", db);
            return table;
            //return "OK";
        }

        [HttpGet("select/{value}")]
        public bool Select(string value)
        {
            return true;
        }

        // POST api/values
        [HttpPost]
        public string Post([FromBody] SQLUnit info)
        {
            return "OK YES";
        }


        // PUT api/values/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/values/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
#endif

        public YcdController()
        {
            if (SQLConnServer.Initialization == false) InitConnection();
        }

        private void InitConnection()
        {
            //初始化所有的数据库连接
            var listConnStr = XmlConfig.GetAllValues("connection.xml");
            for(int i = 0; listConnStr != null && listConnStr.Count > 0 && i < listConnStr.Count; i++)
            {
                SQLConnServer.InitConnection(Encrypt.Decrypt3DES(listConnStr[i]));
            }
        }

        [HttpGet]
        public ActionResult<string> Get()
        {
            return "HELLO API SERVER";
        }

        [HttpPost("lookup")]
        public object Lookup([FromBody] SQLUnit unit)
        {
            var db = SQLConnServer.PopConn(unit.ObjectName);
            var table = SQLCmd.Select(unit.Statement, db);
            return table;
        }

        [HttpPost("change")]
        public int Change([FromBody] SQLUnit unit)
        {
            var db = SQLConnServer.PopConn(unit.ObjectName);
            var chg = SQLCmd.ExecuteSql(unit.Statement, db);
            return chg;
        }

        [HttpPost("getone")]
        public string GetOne([FromBody] SQLUnit unit)
        {
            var db = SQLConnServer.PopConn(unit.ObjectName);
            var res = SQLCmd.ExecuteSqlReturn(unit.Statement, db);
            return res;
        }

    }

}
